package com.wocai.jrt.orgadmin.stats.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wocai.jrt.orgadmin.employee.model.EmployeeGroup;
import com.wocai.jrt.orgadmin.employee.model.Org;
import com.wocai.jrt.orgadmin.employee.model.OrgBean;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeGroupService;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.employee.service.spi.OrgService;
import com.wocai.jrt.orgadmin.stats.bean.EmpStatsMapperReqBean;
import com.wocai.jrt.orgadmin.stats.bean.EmpStatsResBean;
import com.wocai.jrt.orgadmin.stats.service.spi.GroupStatsJobService;
import com.wocai.jrt.orgadmin.stats.service.spi.RedisCacheEmpStatsService;
import com.wocai.jrt.orgadmin.stats.service.spi.RedisCacheGroupStatsService;

@Named
public class GroupStatsJobServiceImpl implements GroupStatsJobService {
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupStatsJobServiceImpl.class);

	@Inject
	private EmployeeGroupService employeeGroupServiceImpl;
	@Inject
	private OrgService orgServiceImpl;
	@Inject
	private RedisCacheGroupStatsService redisCacheGroupStatsServiceImpl;
	@Inject
	private EmployeeService employeeServiceImpl;
	@Inject
	private RedisCacheEmpStatsService redisCacheEmpStatsServiceImpl;

	/**
	 * 每天的定时任务
	 */
	@Override
	public void groupStatsServiceCount() {
		
		//TODO有bug
		DateTime now = new DateTime();
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		DateTimeFormatter formatterYear = DateTimeFormat.forPattern("yyyy");
		DateTime begiDateTime = DateTime.parse("2015-07", formatter);
		LOGGER.info("团队刷新缓存, time=[{}]", now.toString());
		
		// 获取所有机构列表
		OrgBean org = new OrgBean();
		org.createCriteria().andEndTimeGreaterThan(new Date());
		List<Org> orgList = orgServiceImpl.queryForList(org);
		
		// 为所有机构的团队统计数据,这是月数据
		for (Org org2 : orgList) {
			
			// 找出每个机构下所有哦的理财师团队
			EmployeeGroup employeeGroup = new EmployeeGroup();
			employeeGroup.setOrgId(org2.getId());
			employeeGroup.setDeleted(false);
			employeeGroup.setPlannerGroup(true);
			List<EmployeeGroup> employeeGroupList = employeeGroupServiceImpl
					.queryList(employeeGroup);
			for (EmployeeGroup employeeGroup2 : employeeGroupList) {
				DateTime tempDateTime = begiDateTime;
				while (true) {
					redisCacheGroupStatsServiceImpl.queryGroupAppointmentPerformanceCachePut(
							employeeGroup2.getId(), tempDateTime.toString(formatter));
					redisCacheGroupStatsServiceImpl.queryGroupinvestorPerformanceCachePut(
							employeeGroup2.getId(), tempDateTime.toString(formatter));
					redisCacheGroupStatsServiceImpl.queryGroupSalesPerformanceCachePut(
							employeeGroup2.getId(), tempDateTime.toString(formatter));
					tempDateTime = tempDateTime.plusMonths(1);
					if (tempDateTime.toString(formatter).compareTo(now.toString(formatter)) >= 0) {
						break;
					}
				}
				
				redisCacheGroupStatsServiceImpl.queryGroupAppointmentPerformanceCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatter));
				redisCacheGroupStatsServiceImpl.queryGroupinvestorPerformanceCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatter));
				redisCacheGroupStatsServiceImpl.queryGroupSalesPerformanceCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatter));

				// 为所有机构的团队统计数据,这是年数据
				redisCacheGroupStatsServiceImpl.queryGroupAppointmentPerformanceYearCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatterYear));
				redisCacheGroupStatsServiceImpl.queryGroupinvestorPerformanceYearCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatterYear));
				redisCacheGroupStatsServiceImpl.queryGroupSalesPerformanceYearCachePut(
						employeeGroup2.getId(), tempDateTime.toString(formatterYear));
			}
		}
		
		for (Org org2 : orgList) {
			
			// 查出机构下所有的理财师
			EmpStatsMapperReqBean empStatsMapperReqBean = new EmpStatsMapperReqBean();
			empStatsMapperReqBean.setOrgId(org2.getId());
			empStatsMapperReqBean.setStart(null);
			empStatsMapperReqBean.setPageSize(null);
			List<EmpStatsResBean> employeeList = employeeServiceImpl
					.queryAllPlanner(empStatsMapperReqBean);
			
			// 为所有的理财师统计数据
			for (EmpStatsResBean empStatsResBean : employeeList) {
				DateTime tempDateTime = begiDateTime;
				while (true) {
					redisCacheEmpStatsServiceImpl.queryInvestorNumListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					redisCacheEmpStatsServiceImpl.queryAppointmentNumListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					redisCacheEmpStatsServiceImpl.queryDealInvestorListCachePut(empStatsResBean
							.getId());
					redisCacheEmpStatsServiceImpl.queryCheckinNumListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					redisCacheEmpStatsServiceImpl.queryOrdersTotalNumListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					redisCacheEmpStatsServiceImpl.queryOrdersNumListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					redisCacheEmpStatsServiceImpl.queryOrdersAmountListCachePut(
							empStatsResBean.getId(), tempDateTime.toString(formatter));
					tempDateTime = tempDateTime.plusMonths(1);
					if (tempDateTime.toString(formatter).compareTo(now.toString(formatter)) >= 0) {
						break;
					}
					
				}
				
				redisCacheEmpStatsServiceImpl.queryInvestorNumListCachePut(empStatsResBean.getId(),
						tempDateTime.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryAppointmentNumListCachePut(
						empStatsResBean.getId(), tempDateTime.toString(formatter));
				redisCacheEmpStatsServiceImpl
						.queryDealInvestorListCachePut(empStatsResBean.getId());
				redisCacheEmpStatsServiceImpl.queryCheckinNumListCachePut(empStatsResBean.getId(),
						tempDateTime.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersTotalNumListCachePut(
						empStatsResBean.getId(), tempDateTime.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersNumListCachePut(empStatsResBean.getId(),
						tempDateTime.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersAmountListCachePut(
						empStatsResBean.getId(), tempDateTime.toString(formatter));
			}
		}
	}

	/**
	 * 每半小时的定时任务
	 */
	@Override
	public void groupStatsServiceCountHalf() {
		DateTime now = new DateTime();
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		LOGGER.info("团队刷新缓存。。。time=[{}]", now.toString());
		// 获取所有机构列表
		OrgBean org = new OrgBean();
		org.createCriteria().andEndTimeGreaterThan(new Date());
		List<Org> orgList = orgServiceImpl.queryForList(org);
		// 为所有机构的团队统计数据
		for (Org org2 : orgList) {
			// 找出每个机构下所有的理财师团队
			EmployeeGroup employeeGroup = new EmployeeGroup();
			employeeGroup.setOrgId(org2.getId());
			employeeGroup.setDeleted(false);
			employeeGroup.setPlannerGroup(true);
			List<EmployeeGroup> employeeGroupList = employeeGroupServiceImpl
					.queryList(employeeGroup);
			for (EmployeeGroup employeeGroup2 : employeeGroupList) {
				redisCacheGroupStatsServiceImpl.queryGroupAppointmentPerformanceCachePut(
						employeeGroup2.getId(), now.toString(formatter));
				redisCacheGroupStatsServiceImpl.queryGroupinvestorPerformanceCachePut(
						employeeGroup2.getId(), now.toString(formatter));
				redisCacheGroupStatsServiceImpl.queryGroupSalesPerformanceCachePut(
						employeeGroup2.getId(), now.toString(formatter));
			}

		}
		// 为所有机构的理财师统计数据
		for (Org org2 : orgList) {
			
			// 查出机构下所有的理财师
			EmpStatsMapperReqBean empStatsMapperReqBean = new EmpStatsMapperReqBean();
			empStatsMapperReqBean.setOrgId(org2.getId());
			empStatsMapperReqBean.setStart(null);
			empStatsMapperReqBean.setPageSize(null);
			List<EmpStatsResBean> employeeList = employeeServiceImpl
					.queryAllPlanner(empStatsMapperReqBean);
			
			// 为所有的理财师统计数据
			for (EmpStatsResBean empStatsResBean : employeeList) {
				redisCacheEmpStatsServiceImpl.queryInvestorNumListCachePut(empStatsResBean.getId(),
						now.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryAppointmentNumListCachePut(
						empStatsResBean.getId(), now.toString(formatter));
				redisCacheEmpStatsServiceImpl
						.queryDealInvestorListCachePut(empStatsResBean.getId());
				redisCacheEmpStatsServiceImpl.queryCheckinNumListCachePut(empStatsResBean.getId(),
						now.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersTotalNumListCachePut(
						empStatsResBean.getId(), now.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersNumListCachePut(empStatsResBean.getId(),
						now.toString(formatter));
				redisCacheEmpStatsServiceImpl.queryOrdersAmountListCachePut(
						empStatsResBean.getId(), now.toString(formatter));
			}
		}
	}
}
